#include<iostream>
#include<algorithm>
using namespace std;
const int N = 110;
typedef struct
{
	string name;
	int id;
	int y, m, d;
}S;
S s[N];
int n;
int cmp(S a, S b)
{
	if (a.y != b.y) {
		return a.y < b.y;
	}
	else {
		if (a.m != b.m) {
			return a.m < b.m;
		}
		else {
			if (a.d != b.d) {
				return a.d < b.d;
			}
			else {
				return a.id > b.id;
			}
		}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s[i].name >> s[i].y >> s[i].m >> s[i].d;
		s[i].id = i + 1;
	}
	sort(s, s + n, cmp);
	for (int i = 0; i < n; i++) {
		cout << s[i].name << endl;
	}
	return 0;
}